<template>
  <div class="equipment-ledger-page">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-left">
        <h2>
          <el-icon><SetUp /></el-icon>
          设备台账
        </h2>
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/dashboard' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>设备管理</el-breadcrumb-item>
          <el-breadcrumb-item>设备台账</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div class="header-actions">
        <el-button type="primary" @click="handleCreate">
          <el-icon><Plus /></el-icon>
          新增设备
        </el-button>
        <el-button @click="handleImport">
          <el-icon><Upload /></el-icon>
          导入
        </el-button>
        <el-button @click="handleExport">
          <el-icon><Download /></el-icon>
          导出
        </el-button>
        <el-button @click="handlePrintLabel">
          <el-icon><Printer /></el-icon>
          打印标签
        </el-button>
      </div>
    </div>

    <!-- 统计卡片 -->
    <el-row :gutter="20" class="stats-cards">
      <el-col :span="4">
        <el-card shadow="hover" class="stat-card">
          <div class="stat-icon" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);">
            <el-icon><SetUp /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-value">{{ statistics.total }}</div>
            <div class="stat-label">设备总数</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="4">
        <el-card shadow="hover" class="stat-card success">
          <div class="stat-icon" style="background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);">
            <el-icon><CircleCheck /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-value">{{ statistics.running }}</div>
            <div class="stat-label">运行中</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="4">
        <el-card shadow="hover" class="stat-card warning">
          <div class="stat-icon" style="background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);">
            <el-icon><Tools /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-value">{{ statistics.maintenance }}</div>
            <div class="stat-label">维护中</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="4">
        <el-card shadow="hover" class="stat-card danger">
          <div class="stat-icon" style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);">
            <el-icon><Warning /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-value">{{ statistics.fault }}</div>
            <div class="stat-label">故障</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="4">
        <el-card shadow="hover" class="stat-card info">
          <div class="stat-icon" style="background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);">
            <el-icon><SwitchButton /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-value">{{ statistics.idle }}</div>
            <div class="stat-label">闲置</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="4">
        <el-card shadow="hover" class="stat-card">
          <div class="stat-icon" style="background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);">
            <el-icon><TrendCharts /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-value">{{ statistics.oee }}%</div>
            <div class="stat-label">设备OEE</div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 搜索筛选 -->
    <el-card class="search-card" shadow="never">
      <el-form :model="searchForm" :inline="true">
        <el-form-item label="设备编号">
          <el-input v-model="searchForm.code" placeholder="请输入设备编号" clearable style="width: 160px" />
        </el-form-item>
        <el-form-item label="设备名称">
          <el-input v-model="searchForm.name" placeholder="请输入设备名称" clearable style="width: 160px" />
        </el-form-item>
        <el-form-item label="设备分类">
          <el-select v-model="searchForm.category" placeholder="请选择" clearable style="width: 150px">
            <el-option label="数控设备" value="cnc" />
            <el-option label="冲压设备" value="stamping" />
            <el-option label="焊接设备" value="welding" />
            <el-option label="装配设备" value="assembly" />
            <el-option label="检测设备" value="testing" />
            <el-option label="包装设备" value="packing" />
            <el-option label="辅助设备" value="auxiliary" />
          </el-select>
        </el-form-item>
        <el-form-item label="设备状态">
          <el-select v-model="searchForm.status" placeholder="请选择" clearable style="width: 120px">
            <el-option label="运行中" value="running" />
            <el-option label="待机" value="standby" />
            <el-option label="维护中" value="maintenance" />
            <el-option label="故障" value="fault" />
            <el-option label="闲置" value="idle" />
            <el-option label="报废" value="scrapped" />
          </el-select>
        </el-form-item>
        <el-form-item label="所属车间">
          <el-select v-model="searchForm.workshop" placeholder="请选择" clearable style="width: 150px">
            <el-option label="冲压车间" value="stamping_workshop" />
            <el-option label="焊接车间" value="welding_workshop" />
            <el-option label="装配车间" value="assembly_workshop" />
            <el-option label="包装车间" value="packing_workshop" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="handleReset">
            <el-icon><RefreshLeft /></el-icon>
            重置
          </el-button>
          <el-button type="success" @click="handleAdvancedSearch">
            <el-icon><Filter /></el-icon>
            高级筛选
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 设备列表 -->
    <el-card class="table-card" shadow="never">
      <template #header>
        <div class="card-header">
          <span>设备列表</span>
          <div class="header-actions">
            <el-button-group>
              <el-button text @click="handleBatchInspection" :disabled="selectedRows.length === 0">
                <el-icon><View /></el-icon>
                批量巡检
              </el-button>
              <el-button text @click="handleBatchMaintenance" :disabled="selectedRows.length === 0">
                <el-icon><Tools /></el-icon>
                批量维保
              </el-button>
              <el-button text @click="handleBatchExport" :disabled="selectedRows.length === 0">
                <el-icon><Download /></el-icon>
                批量导出
              </el-button>
            </el-button-group>
          </div>
        </div>
      </template>

      <el-table
        :data="tableData"
        v-loading="loading"
        @selection-change="handleSelectionChange"
        stripe
        border
        height="calc(100vh - 520px)"
      >
        <el-table-column type="selection" width="55" fixed />
        <el-table-column prop="code" label="设备编号" width="140" fixed>
          <template #default="{ row }">
            <el-link type="primary" @click="handleView(row)">{{ row.code }}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="设备名称" min-width="160" show-overflow-tooltip />
        <el-table-column prop="category" label="设备分类" width="110">
          <template #default="{ row }">
            <el-tag :type="getCategoryColor(row.category)" size="small">
              {{ getCategoryName(row.category) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="model" label="规格型号" width="150" show-overflow-tooltip />
        <el-table-column prop="workshop" label="所属车间" width="110" />
        <el-table-column prop="location" label="具体位置" width="140" show-overflow-tooltip />
        <el-table-column prop="status" label="设备状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusColor(row.status)" size="small">
              {{ getStatusName(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="oee" label="OEE" width="80" align="center">
          <template #default="{ row }">
            <el-progress 
              :percentage="row.oee" 
              :color="getOEEColor(row.oee)"
              :stroke-width="8"
              :show-text="false"
            />
            <div class="oee-text">{{ row.oee }}%</div>
          </template>
        </el-table-column>
        <el-table-column prop="runningHours" label="运行时长" width="110" align="right">
          <template #default="{ row }">
            {{ row.runningHours }}h
          </template>
        </el-table-column>
        <el-table-column prop="nextMaintenanceDate" label="下次维保" width="120">
          <template #default="{ row }">
            <span :class="{ 'text-warning': isMaintenanceSoon(row.nextMaintenanceDate) }">
              {{ row.nextMaintenanceDate }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="manager" label="负责人" width="90" />
        <el-table-column label="操作" width="280" fixed="right">
          <template #default="{ row }">
            <el-button text type="primary" size="small" @click="handleView(row)">
              <el-icon><View /></el-icon>
              详情
            </el-button>
            <el-button text type="primary" size="small" @click="handleEdit(row)">
              <el-icon><Edit /></el-icon>
              编辑
            </el-button>
            <el-button text type="success" size="small" @click="handleInspection(row)">
              <el-icon><View /></el-icon>
              巡检
            </el-button>
            <el-button text type="warning" size="small" @click="handleMaintenance(row)">
              <el-icon><Tools /></el-icon>
              维保
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handlePageChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import {
  SetUp, Plus, Upload, Download, Printer, CircleCheck, Tools, Warning,
  SwitchButton, TrendCharts, Search, RefreshLeft, Filter, View, Edit
} from '@element-plus/icons-vue'

const router = useRouter()

// 统计数据
const statistics = reactive({
  total: 156,
  running: 132,
  maintenance: 8,
  fault: 3,
  idle: 13,
  oee: 82.5
})

// 搜索表单
const searchForm = reactive({
  code: '',
  name: '',
  category: '',
  status: '',
  workshop: ''
})

// 分页
const pagination = reactive({
  page: 1,
  pageSize: 10,
  total: 0
})

// 表格数据
const tableData = ref([])
const loading = ref(false)
const selectedRows = ref([])

// 模拟数据
const mockData = [
  {
    id: 1,
    code: 'EQ-CNC-001',
    name: '数控车床',
    category: 'cnc',
    model: 'CK6150',
    workshop: '冲压车间',
    location: 'A区-01号位',
    status: 'running',
    oee: 85,
    runningHours: 3256,
    nextMaintenanceDate: '2024-11-15',
    manager: '张三'
  },
  {
    id: 2,
    code: 'EQ-CNC-002',
    name: '加工中心',
    category: 'cnc',
    model: 'VMC850',
    workshop: '冲压车间',
    location: 'A区-02号位',
    status: 'running',
    oee: 88,
    runningHours: 2845,
    nextMaintenanceDate: '2024-12-20',
    manager: '李四'
  },
  {
    id: 3,
    code: 'EQ-WLD-001',
    name: '自动焊接机',
    category: 'welding',
    model: 'AW-500',
    workshop: '焊接车间',
    location: 'B区-01号位',
    status: 'running',
    oee: 78,
    runningHours: 4125,
    nextMaintenanceDate: '2024-10-28',
    manager: '王五'
  },
  {
    id: 4,
    code: 'EQ-STM-001',
    name: '冲压机床',
    category: 'stamping',
    model: 'JH21-160',
    workshop: '冲压车间',
    location: 'C区-01号位',
    status: 'maintenance',
    oee: 72,
    runningHours: 5234,
    nextMaintenanceDate: '2024-10-25',
    manager: '赵六'
  },
  {
    id: 5,
    code: 'EQ-ASM-001',
    name: '自动装配线',
    category: 'assembly',
    model: 'AL-2000',
    workshop: '装配车间',
    location: 'D区-流水线1',
    status: 'running',
    oee: 92,
    runningHours: 1856,
    nextMaintenanceDate: '2025-01-10',
    manager: '钱七'
  },
  {
    id: 6,
    code: 'EQ-TST-001',
    name: '三坐标测量仪',
    category: 'testing',
    model: 'CMM-900',
    workshop: '质检中心',
    location: '检测室',
    status: 'running',
    oee: 68,
    runningHours: 1234,
    nextMaintenanceDate: '2024-11-30',
    manager: '孙八'
  }
]

// 获取列表数据
const fetchList = () => {
  loading.value = true
  
  setTimeout(() => {
    tableData.value = mockData
    pagination.total = mockData.length
    loading.value = false
  }, 500)
}

// 搜索
const handleSearch = () => {
  pagination.page = 1
  fetchList()
}

// 重置
const handleReset = () => {
  Object.assign(searchForm, {
    code: '',
    name: '',
    category: '',
    status: '',
    workshop: ''
  })
  handleSearch()
}

// 高级筛选
const handleAdvancedSearch = () => {
  ElMessage.info('高级筛选功能开发中')
}

// 新增
const handleCreate = () => {
  router.push('/equipment/create')
}

// 查看
const handleView = (row) => {
  router.push(`/equipment/detail/${row.id}`)
}

// 编辑
const handleEdit = (row) => {
  router.push(`/equipment/edit/${row.id}`)
}

// 巡检
const handleInspection = (row) => {
  router.push(`/equipment/inspection?id=${row.id}`)
}

// 维保
const handleMaintenance = (row) => {
  router.push(`/equipment/maintenance?id=${row.id}`)
}

// 批量巡检
const handleBatchInspection = () => {
  ElMessage.info(`已选择 ${selectedRows.value.length} 台设备进行巡检`)
}

// 批量维保
const handleBatchMaintenance = () => {
  ElMessage.info(`已选择 ${selectedRows.value.length} 台设备进行维保`)
}

// 导入
const handleImport = () => {
  ElMessage.info('导入功能开发中')
}

// 导出
const handleExport = () => {
  ElMessage.success('正在导出设备台账...')
}

// 批量导出
const handleBatchExport = () => {
  ElMessage.success('正在导出选中的设备...')
}

// 打印标签
const handlePrintLabel = () => {
  ElMessage.info('打印标签功能开发中')
}

// 选择变化
const handleSelectionChange = (selection) => {
  selectedRows.value = selection
}

// 分页变化
const handleSizeChange = () => {
  fetchList()
}

const handlePageChange = () => {
  fetchList()
}

// 工具函数
const getCategoryName = (category) => {
  const categoryMap = {
    cnc: '数控设备',
    stamping: '冲压设备',
    welding: '焊接设备',
    assembly: '装配设备',
    testing: '检测设备',
    packing: '包装设备',
    auxiliary: '辅助设备'
  }
  return categoryMap[category] || category
}

const getCategoryColor = (category) => {
  const colorMap = {
    cnc: 'primary',
    stamping: 'warning',
    welding: 'danger',
    assembly: 'success',
    testing: 'info',
    packing: '',
    auxiliary: ''
  }
  return colorMap[category] || ''
}

const getStatusName = (status) => {
  const statusMap = {
    running: '运行中',
    standby: '待机',
    maintenance: '维护中',
    fault: '故障',
    idle: '闲置',
    scrapped: '报废'
  }
  return statusMap[status] || status
}

const getStatusColor = (status) => {
  const colorMap = {
    running: 'success',
    standby: 'info',
    maintenance: 'warning',
    fault: 'danger',
    idle: '',
    scrapped: 'danger'
  }
  return colorMap[status] || ''
}

const getOEEColor = (oee) => {
  if (oee >= 85) return '#67c23a'
  if (oee >= 75) return '#e6a23c'
  return '#f56c6c'
}

const isMaintenanceSoon = (date) => {
  if (!date) return false
  const days = Math.ceil((new Date(date) - new Date()) / (1000 * 60 * 60 * 24))
  return days <= 7 && days >= 0
}

// 页面加载
onMounted(() => {
  fetchList()
})
</script>

<style scoped>
.equipment-ledger-page {
  padding: 20px;
  background: #f5f5f5;
  min-height: 100vh;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 20px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}

.header-left h2 {
  margin: 0 0 10px 0;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 24px;
  color: #303133;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.stats-cards {
  margin-bottom: 20px;
}

.stat-card {
  display: flex;
  align-items: center;
  padding: 15px;
  transition: all 0.3s;
}

.stat-card:hover {
  transform: translateY(-3px);
}

.stat-icon {
  width: 56px;
  height: 56px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
  color: white;
  margin-right: 12px;
  flex-shrink: 0;
}

.stat-content {
  flex: 1;
}

.stat-value {
  font-size: 26px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 13px;
  color: #909399;
}

.search-card {
  margin-bottom: 20px;
}

.table-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.oee-text {
  font-size: 12px;
  color: #606266;
  margin-top: 4px;
  text-align: center;
}

.text-warning {
  color: #e6a23c;
  font-weight: 600;
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
</style>

